/**
 *
 */
package com.newer.controller.sys.user;

import com.newer.biz.sys.user.UserInfoBiz;
import com.newer.commons.result.BizResult;
import com.newer.dao.bean.sys.Role;
import com.newer.dao.bean.sys.User;
import com.newer.dao.bean.sys.UserRole;
import net.sf.json.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * 用户信息Controller
 *
 * @author xuntj
 * @create 2018-03-14 14:43
 **/
@Controller
@RequestMapping("sys/user")
public class UserController {

    /**
     * 用户信息业务操作类
     */
    @Autowired
    UserInfoBiz userInfoBiz;

    /**
     * 菜单列表默认展示
     *
     * @return
     */
    @RequestMapping("list.htm")
    public String user() {
        return "sys/user/user_list";
    }


    /**
     * 用户信息分页查询
     *
     * @return
     */
    @RequestMapping("doList.do")
    @ResponseBody
    public String userList(User user) {
        BizResult<?> bizResult = userInfoBiz.queryUserInfoByPages(user);
        return JSONObject.fromObject(bizResult).toString();
    }


    /**
     * 用户详细信息查询
     *
     * @return
     */
    @RequestMapping("detail.htm")
    public String userList(String userId, ModelMap map) {
        BizResult<User> bizResult = userInfoBiz.queryUserDetail(userId);
        if (bizResult.isSuccess()) {
            map.put("user", bizResult.getResult());
        }
        return "sys/user/user_detail";
    }


    /**
     * 用户信息授权
     *
     * @return
     */
    @RequestMapping("/author.htm")
    public String author() {
        return "sys/user/user_author";
    }

    /**
     * 角色信息列表默认展示
     *
     * @return
     */
    @RequestMapping("/doRoleList.do")
    @ResponseBody
    public String roleList(@RequestParam(defaultValue = "") String roleName, String id, Integer pageNo, Integer pageSize) {
        BizResult<Role> bizResult = userInfoBiz.userRoleExits(roleName, id, pageNo, pageSize);
        return JSONObject.fromObject(bizResult).toString();
    }

    /**
     * 角色信息列表默认展示
     *
     * @return
     */
    @RequestMapping("/userRoleGrant.do")
    @ResponseBody
    public String userRoleGrant(UserRole userRole) {
        BizResult<?> bizResult = userInfoBiz.userRoleGrant(userRole);
        return JSONObject.fromObject(bizResult).toString();
    }

    /**
     * 角色信息列表默认展示
     *
     * @return
     */
    @RequestMapping("/changeStatus.do")
    @ResponseBody
    public String userInfoStateChange(String userId, Integer status) {
        BizResult<?> bizResult = userInfoBiz.userInfoStateChange(userId, status);
        return JSONObject.fromObject(bizResult).toString();
    }


    /**
     * 用户信息授权
     *
     * @return
     */
    @RequestMapping("/operate.htm")
    public String operate(@RequestParam(defaultValue = "") String id, ModelMap map) {
        // 判断是否为空
        if (StringUtils.isNotBlank(id)) {
            BizResult<User> userBizResult = userInfoBiz.queryUserDetail(id);
            if (userBizResult.isSuccess()) {
                map.put("user", userBizResult.getResult());
            }
        }
        return "sys/user/user_operate";
    }

    /**
     * 用户信息授权
     *
     * @return
     */
    @RequestMapping("/doOperate.do")
    @ResponseBody
    public String doOperate(User user) {
        BizResult<?> bizResult = userInfoBiz.operateUser(user);
        return JSONObject.fromObject(bizResult).toString();
    }

    /**
     * 用户信息授权
     *
     * @return
     */
    @RequestMapping("/doRestPwd.do")
    @ResponseBody
    public String doRestPwd(String id, @RequestParam(defaultValue = "") String pwd) {
        BizResult<?> bizResult = userInfoBiz.resetPwd(id, pwd);
        return JSONObject.fromObject(bizResult).toString();
    }

}
